import 'dart:convert';

import 'package:flui/flui.dart';
import 'package:flutter/material.dart';
import 'package:fzcgx_app/network/http.dart';
import 'package:fzcgx_app/router/routes.dart';
import 'package:fzcgx_app/ui/base/base_view_model.dart';
import 'package:fzcgx_app/ui/my/factory/model/factory_device_model.dart';
import 'package:fzcgx_app/ui/my/factory/page/factory_device_edit_page.dart';

class FactoryDeviceViewModel extends BaseViewModel {
  List<FactoryDeviceModel> selectModels;
  List<FactoryDeviceModel> models;
  bool isInput = false;
  TextEditingController nameController = TextEditingController();
  FactoryDeviceViewModel() {
    this.setState(ViewState.Loading);
    selectModels = List<FactoryDeviceModel>();
  }
  void requestApi() {
    Http.instance.postList<FactoryDeviceModel>('equip/index', null,
        onSuccess: (data) {
      if (null != data) {
        this.models = data;
        updateSelectModes();
        this.setState(ViewState.Success);
      } else {
        models = List<FactoryDeviceModel>();
        this.setState(ViewState.Success);
      }
    }, onError: (error) {
      this.setState(ViewState.Error);
    }, onFail: (fail) {
      this.setState(ViewState.Error);
    });
  }

  void onAdd() {
    if (this.nameController.text.toString().isEmpty) {
      // FLToast.showText(text: '请输入款式名称');
      return;
    }
    var dismiss = FLToast.loading(text: '正在添加...');
    Http.instance.post('equip/add', {
      'name': this.nameController.text,
    }, onSuccess: (data) {
      this.nameController.text = '';
      FLToast.showSuccess(text: '添加成功');
      dismiss();
      requestApi();
    }, onError: (error) {
      dismiss();
    }, onFail: (fail) {
      dismiss();
    }, complete: () {});
  }

  void onInput() {
    this.isInput = !this.isInput;

    notifyListeners();
  }

  void onSelectChip(BuildContext context,FactoryDeviceModel model) {
    if (model.check || model.isedit == 0) {
       Routes.navigateTo(context, FactoryDeviceEditPage(model: model,callback: (){
         requestApi();
       },));
      return;
    }

    model.check = !model.check;
    updateSelectModes();
    notifyListeners();
  }

  void updateSelectModes() {
    selectModels.clear();
    this.models.forEach((element) {
      if (element.check) {
        this.selectModels.add(element);
      }
    });
    notifyListeners();
  }
  void delChip(FactoryDeviceModel delModel){
    delModel.check=false;
    this.selectModels.remove(delModel);
    notifyListeners();
  }
  void postApi(BuildContext context,Function callback){
    
     if (selectModels.length == 0) {
      FLToast.showText(text: '请选择产品，选择不上限');
      return;
    }
    var dismiss = FLToast.loading(text: '正在保存...');
    Http.instance.post('equip/check-save',{'ids':getSelectModelsStr()} , onSuccess: (data) {
      FLToast.showSuccess(text:'保存成功');
      callback();
      Navigator.pop(context);
    }, onError: (error) {}, onFail: (fail) { },complete: (){
      dismiss();
    });
  }
  String getSelectModelsStr(){
    List ids=List();
    this.selectModels.forEach((element) {
      ids.add(element.id);
    });
    return jsonEncode(ids);
  }
}
